// let listLoad = require('../../common/ListLoad')
Page({
    // behaviors: [listLoad],
    data: {
        day: '', //今日日期
        typeList: [{ name: '按单品', index: 0, color: 'red' }, { name: '按品种', index: 1, color: '#333' }],
        type_popup: false, //展示按品种、单品选择框
        typeActive: 0,
        shelfList: [], //货位号列表 
        shelfActive: null, //选中的货架号索引
        inList: [{ name: '未入位', index: 0, color: 'red' }, { name: '已入位', index: 1, color: '#333' }],
        in_popup: false, //展示未入位、已入位选择框
        // nums: 0, //入位、未入位数量
        inActive: 0,
        list: [],
        loading: false,
        finished: false,
        empty: false,
    },
    onLoad: function(options) {
        this.setData({ day: options.day.replaceAll('-', '') })
        this.getInfo()
        this.getShelf()
    },
    getInfo() {
        this.setData({
            list: [],
            empty: true,
            loading: true,
            finished: false
        })
        const { day, typeActive, shelfList, shelfActive, inActive } = this.data
        let reqData = {
            show_type: typeActive,
            type: inActive,
            day: day
        }
        if (shelfActive > 0) {
            reqData.shelve_type = shelfList[shelfActive].shelve_title
            reqData.shelve_num = shelfList[shelfActive].shelve_num
        }
        this.request({
            url: '/supplier/order/peiInfo',
            data: reqData
        }, res => {
            if (typeActive === 0) { //按单品
                res.data.forEach(item => {
                    item.in = inActive === 1
                })
            } else { //按种类
                res.data.forEach(item => {
                    item.order_item.forEach(child => {
                        child.in = inActive === 1
                    })
                })
            }
            this.setData({
                list: res.data,
                empty: res.data.length === 0,
                // nums: res.data.length,
                finished: true,
                loading: false
            })
        })
    },
    /**获取货架号列表 */
    getShelf() {
        this.request({
            url: '/supplier/order/shelve_num?day=' + this.data.day
        }, res => {
            res.data.unshift({
                shelve_title: '全部',
                shelve_num: ''
            })
            res.data.forEach(item => {
                item.name = item.shelve_title + item.shelve_num
            })
            this.setData({ shelfList: res.data })
        })
    },
    /**选择类型 */
    selectType({ detail }) {
        const { typeActive } = this.data
        this.setData({
            typeActive: detail.index,
            type_popup: false,
            ['typeList[' + typeActive + '].color']: '#333',
            ['typeList[' + detail.index + '].color']: 'red',
        })
        typeActive === detail.index || this.getInfo()
    },
    /**选择货架号 */
    bindPickerShelf(e) {
        const value = Number(e.detail.value)
        this.setData({ shelfActive: value })
        this.getInfo()
    },
    /**选择入位 */
    selectIn({ detail }) {
        const { inActive } = this.data
        this.setData({
            inActive: detail.index,
            in_popup: false,
            ['inList[' + inActive + '].color']: '#333',
            ['inList[' + detail.index + '].color']: 'red',
        })
        inActive === detail.index || this.getInfo()
    },
    /**点击全部入位 */
    inAll() {
        if (this.data.inActive === 1) return
        if (this.data.list.length === 0) return wx.showToast({
            title: '暂无可入位商品',
            icon: 'none'
        })
        wx.$dialog.confirm({
            title: '提示',
            message: '您确定将所有商品全部入位吗？',
            theme: 'round-button',
            beforeClose: (action) => new Promise((resolve) => {
                if (action === 'confirm') {
                    const { list, typeActive } = this.data
                    let order_product_ids = []
                    if (typeActive === 0) { //按单品
                        order_product_ids = list.map(item => {
                            return item.order_product_id
                        })
                    } else { //按种类
                        list.forEach(list => {
                            list.order_item.forEach(order => {
                                order_product_ids.push(order.order_product_id)
                            })
                        })
                    }

                    return this.request({
                        url: '/supplier/order/delivered',
                        method: 'POST',
                        data: {
                            order_product_ids
                        }
                    }, () => {
                        resolve(true)
                        wx.showToast({
                            title: '入位成功',
                            icon: 'success',
                            mask: true
                        })
                        this.delay(() => {
                            this.setData({ inActive: 1 })
                            this.getInfo()
                        })
                    }, () => {
                        resolve(true)
                    })
                }
                resolve(true)
            })
        })
    },
    /**单个入位 */
    operation({ mark }) {
        if (this.data.inActive === 1) return
        const { index, order_index } = mark
        if (index == undefined && order_index == undefined) return
        wx.$dialog.confirm({
            title: '提示',
            message: '您确定设置入位吗？',
            theme: 'round-button',
            beforeClose: (action) => new Promise((resolve) => {
                if (action === 'confirm') {
                    const { list, typeActive } = this.data
                    let order_product_ids = []
                    if (typeActive === 0) { //按单品
                        order_product_ids.push(list[index].order_product_id)
                    } else { //按种类
                        order_product_ids.push(list[index].order_item[order_index].order_product_id)
                    }
                    return this.request({
                        url: '/supplier/order/delivered',
                        method: 'POST',
                        data: {
                            order_product_ids
                        }
                    }, () => {
                        resolve(true)
                        wx.showToast({
                            title: '入位成功',
                            icon: 'success',
                            mask: true
                        })
                        if (typeActive === 0) { //按单品
                            this.setData({
                                ['list[' + index + '].in']: true
                            })
                        } else { //按种类
                            this.setData({
                                ['list[' + index + '].order_item[' + order_index + '].in']: true
                            })
                        }
                        // this.computeSum(index)
                    }, () => {
                        resolve(true)
                    })
                }
                resolve(true)
            })
        })
    },
    /**计算未入位数量 */
    // computeSum(index) {
    //     const { list, typeActive, nums } = this.data
    //     if (typeActive === 0) { //按单品
    //         this.setData({ nums: nums - 1 })
    //     } else { //按种类
    //         let allIn = list[index].order_item.every(item => {
    //             return item.in === true
    //         })
    //         allIn && this.setData({ nums: nums - 1 })
    //     }
    // }
})